<?php 

    /**
     * This file is part of the Intelligencer package.
     * For the full copyright and license, please view the LICENSE
     * file that was distributed with this source code.
     * 
     * @author      drapeko <roman.drapeko@gmail.com>
     * @copyright   Roman Drapeko <roman.drapeko@gmail.com>, 2009 - present
     */

    /**
     * This class is responsible for loading the Intelligencer
     * @author drapeko
     */
    class itLoader {
        
        protected static $classes = array(
            'itIntelligencer'                 => 'Intelligencer',
            'itRulesIntelligencer'            => 'Intelligencer',
            'itTemplateIntelligencer'         => 'Intelligencer',
            'itCache'                         => 'LibUtil/Cache',
            'itLocalCache'                    => 'LibUtil/Cache',
            'itPersistentCache'               => 'LibUtil/Cache',
            'itPersistentCacheableI'          => 'LibUtil/Cache',
            'itException'                     => 'LibUtil/Exception',
            'itParameterException'            => 'LibUtil/Exception',
            'itNullRulesException'            => 'LibUtil/Exception',
            'itScanner'                       => 'LibUtil/Scanner',
            'itScannerI'                      => 'LibUtil/Scanner',
            'itScannerResultI'                => 'LibUtil/Scanner',
            'itScannerResult'                 => 'LibUtil/Scanner',
            'itLoggerI'                       => 'LibUtil/Logger',
            'itPearLogger'                    => 'LibUtil/Logger',
            'itUtil'                          => 'LibUtil',
            'itManager'                       => 'Manager',
            'itRules'                         => 'Rules',
            'itClassRules'                    => 'Rules',
            'itClassRulesConvertibleI'        => 'Rules',
            'itComplexRules'                  => 'Rules',
            'itSimpleRules'                   => 'Rules',
            'itSimpleRulesConvertibleI'       => 'Rules',
            'itSimpleTemplate'                => 'Template',
            'itTemplate'                      => 'Template',
            'itZendStyleTemplate'             => 'Template',
        );
        
        protected static $lowerClasses = null;
        
        protected static $loader = null;

        protected static function doLowerClasses() {
            $ds = DIRECTORY_SEPARATOR;
            foreach(self::$classes as $class => $dir) {
                self::$lowerClasses[strtolower($class)] = $dir.$ds.$class.'.php';
            }
        }
        
        public static function register() {
            self::doLowerClasses();
            spl_autoload_register(array(__CLASS__, 'autoload'));
        }
        
        public static function unregister() {
            spl_autoload_unregister(array(__CLASS__, 'autoload'));
        }
        
        public static function autoload($className) {
            if (substr(__CLASS__, 0, 2) == 'it') {
                $lowerClass = strtolower($className);
                if (array_key_exists($lowerClass, self::$lowerClasses)) {
                    $ds = DIRECTORY_SEPARATOR;
                    $file = dirname(__FILE__).$ds.self::$lowerClasses[$lowerClass];
                    if (is_file($file)) {
                        require_once $file;
                    }
                }
            }
        }
    }

    itLoader::register();

?>